package com.grad.why.studentsystem.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.grad.why.studentsystem.domain.ClassRoom;
import com.grad.why.studentsystem.domain.Classinfo;
import com.grad.why.studentsystem.domain.Course;
import com.grad.why.studentsystem.domain.current.CurrentClassinfo;
import com.grad.why.studentsystem.mapper.ClassRoomMapper;
import com.grad.why.studentsystem.service.ClassRoomService;
import com.grad.why.studentsystem.util.Util;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

@Component
@Service(interfaceClass = ClassRoomService.class)
@Transactional
public class ClassRoomServiceImpl implements ClassRoomService {
    @Autowired
    private ClassRoomMapper classRoomMapper;

    @Override
    public List<ClassRoom> getClassRoomInfoList(int offset, int limit, String search) {
        try{
            return classRoomMapper.getClassRoomInfoList(offset,limit,search);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long getClassRoomInfoListLength(String search) {
        try{
            return classRoomMapper.getClassRoomInfoListLength(search);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteClassRoomInfosById(int rid) {
        try{
            return classRoomMapper.deleteByPrimaryKey(rid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int getMaxrid() {
        try{
            return classRoomMapper.getMaxrId();
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int addClassRoomInfo(ClassRoom classRoom) {
        try{
            return classRoomMapper.insertSelective(classRoom);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int isClassRoomExist(String classRoomName) {
        try{
            int count = classRoomMapper.classRommIsExist(classRoomName);
            return count;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<ClassRoom> getClassRoomInfos() {
        try {
            return classRoomMapper.getClassRoomInfos();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int addClassRoomInfosByExcelFile(String uploadCurrentFileName) {
        try{
            InputStream is = new FileInputStream(uploadCurrentFileName);
            Workbook wb = null;

            if(uploadCurrentFileName.split("\\.")[1].equals("xls")){
                wb = new HSSFWorkbook(is);
            }else if(uploadCurrentFileName.split("\\.")[1].equals("xlsx")){
                wb = new XSSFWorkbook(is);
            }
            int sheetNumber = wb.getNumberOfSheets();
            int InsertCount = 0;

            Row title = wb.getSheetAt(0).getRow(0);
            //修改
            if(!title.getCell(0).getStringCellValue().equals("教室名称")||
                    !title.getCell(1).getStringCellValue().equals("教室容量")||
                    !title.getCell(2).getStringCellValue().equals("教室状态")){
                return 0;
            }
            for(int i = 0;i<sheetNumber;i++) {
                int index = 1;
                for(Row row : wb.getSheetAt(i)){
                    if(row == null){
                        continue;
                    }else{
                        if(index == 1){

                        }else{
                            if(row.getCell(0) == null){
                                index++;
                                continue;
                            }else{
                                ClassRoom classRoom = new ClassRoom();
                                classRoom.setRid(classRoomMapper.getMaxrId() + 1 );
                                classRoom.setClassroomname(Util.formGetCellType(row.getCell(0)));
                                classRoom.setClassroomcapcity(Integer.parseInt(Util.formGetCellType(row.getCell(1))));
                                String state = Util.formGetCellType(row.getCell(2));

                                if(state.equals("正在使用")){
                                    classRoom.setState(1);
                                }else if(state.equals("未使用")){
                                    classRoom.setState(0);
                                }

                                ClassRoom c = classRoomMapper.selectByPrimaryKey(classRoom.getRid());

                                classRoomMapper.insertSelective(classRoom);
                                InsertCount = InsertCount + 1;
                            }
                        }
                        index++;
                    }
                }
            }
            return InsertCount;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }
}
